document.write("<link href='"+appRoot+"script/report/css/report.css' type=text/css rel=stylesheet>");

function Crosstab(){

	this.data = null;
	this.rowTitles = null;
	this.rowLayouts = null;
	
	this.setData = function(data){
		this.data = data;
	}
	
	
	
	this.show = function(divId){
		if (this.data==null) return;
		var data = this.data
		$(divId).innerHTML = data.table;
		$(divId).getElement('table').set({cellpadding:'3',width:'90%'})
	}
	
}


/**
 * 
 * @param {Object} div - 报表所在DIV
 * @param {Object} sql - 报表依据SQL
 * @param {Object} cc - CrossTabConf的初始化元素
 * @param {Object} initPivotHead - 自定义PivotHead的方法
 * @param {Object} disableConf - 关闭CrossTabConf
 * @param {Object} backFunc - 返回函数
 */
Pivot = function(div,sql,cc,option){//cc:CrossConf
//	if (Pivot[div]){
//		showWarning("重复报表定义");
//		return;
//	}
	if (!option)
		option = {};

	Pivot[div] = this;
	this.div = div;
	this.sql = sql;
	this.conf = new CrossConf();

	this.initDiv();
	$(this.div+'_conf').onclick = function(){Pivot[div].conf.openCrossConfDlg(function(){Pivot[div].saveConf()})};
	$(this.div+'_table').onclick = function(){Pivot[div].showTable()};
	$(this.div+'_chart').onclick = function(){Pivot[div].showChart(1)};
	$(this.div+'_chart2').onclick = function(){Pivot[div].showChart(2)};
	$(this.div+'_export').onclick = function(){Pivot[div].exportFile();return false;};

	$(this.div+'_preview').onclick = function(){WebPrint.preview(div+'_printArea',option.print)};
	$(this.div+'_print').onclick = function(){WebPrint.print(div+'_printArea',option.print)};
	if (!option.backFunc)
		$(this.div+'_back').onclick = function(){switchPage('queryView')};
	else
		$(this.div+'_back').onclick = option.backFunc;
	//$(this.div+'_back2').onclick = $(this.div+'_back').onclick;
	
	if (option.disableConf)
		$(this.div+'_conf').style.display='none';
	if (option.disableBack)
		$(this.div+'_back').style.display='none';
	if (option.disableChart){
		$(this.div+'_table').style.display='none';
		$(this.div+'_chart').style.display='none';
		$(this.div+'_chart2').style.display='none';
	}
	if(cc)
		this.initConf(cc);
	if (option.initPivotHead)
		option.initPivotHead(this.div);
	else
		this.initPivotHead();
	
	this.ShowSummary = false;		//图:不显示汇总项
	this.ShowZero = false;			//图:不显示空项
	this.viewMode = this.showTable;	//默认的模式
	this.param = {};
//	this.refresh();
}
Pivot.prototype.initDiv = function(){
	var s = "<div class='form'><div class='reportBar treeBtn'>"
		+ " <a href='javascript:void(0)' id='#DIVID#_conf'><img src='../images/button/custom.gif' title='定制' align='absmiddle'>定制</a>"
		+ " <a href='javascript:void(0)' id='#DIVID#_table'><img src='../images/button/report.gif' align=absmiddle title='报表' align='absmiddle'>报表</a>"
		+ " <a href='javascript:void(0)' id='#DIVID#_chart'><img src='../images/button/histogram.gif' title='柱状图' align='absmiddle'>柱状图</a>"
		+ " <a href='javascript:void(0)' id='#DIVID#_chart2'><img src='../images/button/pie.gif' title='饼状图' align='absmiddle'>饼状图</a>"
		+ " <a href='javascript:void(0)' id='#DIVID#_preview'><img src='../images/button/preview.gif' title='打印预览' align='absmiddle'>打印预览</a>"
		+ " <a href='javascript:void(0)' id='#DIVID#_print'><img src='../images/button/print.gif' title='打印' align='absmiddle'>打印</a>"
		+ " <a href='javascript:void(0)' id='#DIVID#_export'><img id='#DIVID#_export' src='../images/button/export.gif' title='导出' align='absmiddle'>导出</a>"
		+ " <a href='javascript:void(0)' id='#DIVID#_back'><img id='#DIVID#_back' src='../images/button/cancel.gif' title='返回' align='absmiddle'>返回</a>"
		+ " <span id='#DIVID#_measures' style='display:none'><label>统计量&nbsp;</label><select id='#DIVID#_select'></select></span></div>"
		+ " <div id='#DIVID#_printArea'><div id='#DIVID#_pivot_head' align='center'></div><div align='center' id='#DIVID#_pivot_body'></div><div align='center' id='#DIVID#_pivot_tail'></div></div>"
		+ " <div id='#DIVID#_flash'></div><br></div>";
		//+"<p><button type='button' id='#DIVID#_back2'>返 回</button></p>";
	$(this.div).innerHTML = s.replace(/#DIVID#/g,this.div);
}
Pivot.prototype.initPivotHead = function(){
	var title = '<br><table width="100%" border="0" cellspacing="0" cellpadding="5" style="width:700px;margin-bottom:6px;">'
			  + '<tr><td align="center" class="reportTitle" height="50" colspan="2" id="#DIVID#_pivotTitle"></td></tr>'
			  + '<tr><td width="60%">统计机构：<span id="#DIVID#_statOrg"></span></td><td>统 计 人：<span id="#DIVID#_statOper"></span></td></tr>'
			  + '<tr><td>起始日期：<span id="#DIVID#_beginTime"></span></td><td>结束日期：<span id="#DIVID#_endTime"></span></td></tr>'
			  + '<tr><td>统计时间：<span id="#DIVID#_statTime"></span></td><td>统计依据：<span id="#DIVID#_dateType"></td></tr></table>';//报表类型：<span id="#DIVID#_statClass"></span>
	$(this.div+'_pivot_head').innerHTML = title.replace(/#DIVID#/g,this.div);
	var tail = "<div class='right' style='width:700px'><br><table width='100%' cellspacing=0 cellpadding=2 class='col3'><tr><td></td><td align=center>复核人:______________</td><td align=right>制表人:______________</td></tr></table></div>";
	$(this.div+'_pivot_tail').innerHTML = tail;
	this.setPivotHead('pivotTitle',document.title);
	this.setPivotHead('statOper',Common.cOperator().name);
	this.setPivotHead('statOrg',Common.cOrg().name);
}
Pivot.prototype.setPivotHead = function(key,value){
	if ($(this.div+'_'+key))
		$(this.div+'_'+key).innerHTML = value;
}
Pivot.prototype.getPivotHead = function(key){
	return $(this.div+'_'+key)
}
Pivot.prototype.hidePivotHead = function(key){
	var obj = $(this.div+'_'+key)
	if (obj){
		obj.getParent().style.display = "none"; 
	}
}
Pivot.prototype.initConf = function(cc){
	if (!cc.defaultMeasure)//此处只是通过给对象指定一个值,而不是严格意义的true值
		cc.defaultMeasure = cc.measures;
	this.cc = cc;
	this.conf.initCrosstab(cc.dimensions,cc.measures,cc.defaultHorizon,cc.defaultVertical,cc.defaultMeasure);
	var list = [];
	$H(cc.measures).each(function(v,x){
		list.push({code:x,text:v});
	});
	var defCode = this.conf.getChartMeasures();
	defCode = defCode.substring(1,defCode.indexOf(';'));
	var pivot = Pivot[this.div];
	Common._initSelect($(this.div+'_select'),{list:list,defCode:defCode,onchange:function(){pivot.refresh()}});
}
Pivot.prototype.saveConf = function(){
	this.cc.defaultMeasure = this.conf.getCurrentMeasure();
	this.cc.defaultVertical = this.conf.getCurrentVertical();
	this.cc.defaultHorizon = this.conf.getCurrentHorizon();
	this.refresh();
}
Pivot.prototype.refresh = function(){
	if(this.cc)
		this.viewMode();
}

Pivot.prototype.showTable = function(){
	this.viewMode = this.showTable;
	$(this.div+'_flash').style.display='none';
	$(this.div+'_measures').style.display='none';
	$(this.div+'_printArea').style.display='';
	var rows = this.conf.getDimensions();
	var cols = this.conf.getMeasures();//Common.getValue($(this.div+'_select'))//
	var target = this.div+'_pivot_body';
	var obj = DWR.syncCall("crosstabService.getTable", this.sql, rows, cols, this.param);
	var crosstab = new Crosstab();
	crosstab.setData(obj);
	crosstab.show(target);
	
}
Pivot.prototype.showChart = function(type){
	this.viewMode = this.showChart;
	if (type)
		this.chartType = type;
	$(this.div+'_printArea').style.display='none';
	$(this.div+'_measures').style.display='';
	$(this.div+'_flash').style.display='';
	var rows = this.conf.getChartDimensions();
	var measure = ($(this.div+'_select')).value;
	var cols = '|'+measure+';';//this.conf.getChartMeasures();
	var obj = DWR.syncCall("crosstabService.statData", this.sql, rows, cols, this.param);
	this.FusionCharts(obj);
}
Pivot.prototype.FusionCharts = function (obj){
	var skipSum = !this.ShowSummary;
	var skipZero = !this.ShowZero;
	var rows = obj.rowTitles;
	var columns = obj.columnTitles;
	var data = obj.dataMatrix;
	if (data.length != obj.measureSize){
		showError('数据错误:数据集维度不一致');
		return;
	}
	var measure = obj.dimensions.slice(-1*obj.measureSize);
	//var chartData = new Array();
	var chartData = FusionChartDateTransformer(obj,skipSum,skipZero);
//	if (obj.rowSize==0 && obj.columnSize==0){
//		showError("未设置统计维度");
//		return;
//	}else if (obj.rowSize==0 || obj.columnSize==0){
//		var single = rows.length==0?columns:rows;
//		creation:
//			for(var j=0;j<single.length;j++){
//				var x = single[j];
//				if (skipSum)
//					for(var t=0;t<x.length;t++)
//						if(x[t]==null)
//							continue creation;
//				var d = '['+x.join('][')+']';
//				for(var k=0;k<measure.length;k++){
//					if (skipZero && data[k][j]==0)
//						continue creation;
//					chartData.push([d,measure[k].title,data[k][j]]);
//				}
//			}
//	}else{
//		creation:
//			for(var i=0;i<rows.length;i++){
//				for(var j=0;j<columns.length;j++){
//					var x = rows[i].concat(columns[j]);
//					if (skipSum)
//						for(var t=0;t<x.length;t++)
//							if(x[t]==null)
//								continue creation;
//					var d = x.join();
//					for(var k=0;k<measure.length;k++){
//						if (skipZero && data[k][i][j]==0)
//							continue creation;
//						chartData.push([d,measure[k].title,data[k][i][j]]);
//					}
//				}
//			}
//	}
	if(chartData.data[0] == null){
		//showWarning('没有数据');
		$(this.div+'_flash').innerHTML = '没有数据';
		return;
	}
	
	if(this.chartType == 1)
		this.chart = new FusionCharts2DColumn();//可能和measure的个数有关,所以在方法内赋值
	else if(this.chartType == 2)
		this.chart = new FusionChart2DPie();
	this.chart.div = this.div+'_flash';
	
	//this.chart.setAndShow(chartData);
	var length = obj.dimensions.length;
	var measure = obj.dimensions[length-1].name;
	//var unit = (obj.dimensions[length-1].name=='count')?'':'Yuan元'
	var xAxis = '';
	for(var i = 0;i<length-1;i++)
		xAxis += '['+obj.dimensions[i].title+']'
	this.chart.setSingleData(chartData);
	//this.chart.setAttribute("graph@caption",chartData[0][1]);
	//this.chart.setAttribute("graph@yAxisName",unit);
	this.chart.setAttribute("graph@xAxisName",xAxis);
	if(measure=='volumn'){
		this.chart.setAttribute("graph@yAxisName","Litre");
		this.chart.setAttribute("graph@decimalPrecision",2);
	}else if(measure=='point' || measure=='negPoint'
				|| (measure.toLowerCase()).indexOf('count')>-1
				|| (measure.toLowerCase()).indexOf('sum')>0
				|| measure=='endAmount'
				|| measure=='deliveryAmount'
				|| measure=='warehousingAmount'
				|| measure=='adjustAmount'
				){
		this.chart.setAttribute("graph@decimalPrecision",0);
	}else{
		this.chart.setAttribute("graph@yAxisName","Yuan");
		this.chart.setAttribute("graph@numberPrefix",'￥');
		this.chart.setAttribute("graph@decimalPrecision",2);
	}
//	this.chart.setAttribute("graph@outCnvBaseFont","simsun");
//	this.chart.setAttribute("graph@rotateNames",1);
//	this.chart.setAttribute("graph@showNames",1);
	
	//alert(this.chart.getXML());
	this.chart.show();
	return;
	
}

Pivot.prototype.exportFile = function (obj){
	var params = clone(this.param)
	
	params.rows = this.conf.getDimensions();
	params.columns = this.conf.getMeasures()
	params.exportType = 'xls'
	params.queryName = this.sql

	Eui.exportCrosstabReport(params)

}